package org.shareworth.online_life.client.module.login;

import org.shareworth.online_life.client.mvc.AppEvents;
import org.shareworth.online_life.client.util.FixedFormPanel;
import org.shareworth.online_life.client.util.JSONRequest;

import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.mvc.Dispatcher;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.InfoConfig;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.RequestException;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.Element;

public class LoginDialog extends Dialog {

	protected TextField<String> username;
	protected TextField<String> password;
	protected Button regist;
	protected Button login;
	protected Status status;

	protected FormPanel formPanel;

	public LoginDialog() {

		formPanel = new FixedFormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setBorders(false);
		formPanel.setBodyBorder(false);

		FormLayout layout = new FormLayout();
		formPanel.setFrame(false);
		layout.setLabelWidth(90);
		layout.setDefaultWidth(155);
		formPanel.setLayout(layout);
		add(formPanel);
		setClosable(false);
		setButtonAlign(HorizontalAlignment.LEFT);
		setHeading("请登陆");
		setModal(true);
		setBodyBorder(true);
		setBodyStyle("padding: 8px;background: none");
		setWidth(300);
		setResizable(false);
		setButtons("");

		formPanel.setAction("test.html");
		
		
		username = new TextField<String>();
		username.setValue("admin");
		username.setName("userName");
		username.setFieldLabel("用户名");
		formPanel.add(username);

		password = new TextField<String>();
		password.setValue("123");
		password.setName("passWord");
		password.setFieldLabel("密码");
		password.setPassword(true);
		formPanel.add(password);

		setFocusWidget(username);
	}
	@Override
	protected void onRender(Element parent, int pos) {
		// TODO Auto-generated method stub
		super.onRender(parent, pos);
	}

	@Override
	protected void createButtons() {
		super.createButtons();
		status = new Status();

		status.hide();
		status.setAutoWidth(true);
		getButtonBar().add(status);

		getButtonBar().add(new FillToolItem());

		regist = new Button("注册");
		regist.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				LoginDialog.this.hide();
				RegistDialog registDialog = new RegistDialog();
				registDialog.loginDialog = LoginDialog.this;
				registDialog.show();
			}
		});

		login = new Button("登陆");
		login.addSelectionListener(new SelectionListener<ButtonEvent>() {
			@Override
			public void componentSelected(ButtonEvent ce) {
				//formPanel.submit();
				JSONRequest jsonrRequest = new JSONRequest("test.do",new RequestCallback(){

					@Override
					public void onError(Request request, Throwable exception) {
						// TODO Auto-generated method stub
						MessageBox.alert("", "xxx", null);
					}

					@Override
					public void onResponseReceived(Request request,
							Response response) {
						onLoginSuccess(response.getText());
						
					}
					
				});
				try {
					jsonrRequest.sendRequest();
				} catch (RequestException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

		});

		addButton(regist);
		addButton(login);
	}

	private void onLoginSuccess(String result){
		 Registry.register("person", result);
		 LoginDialog.this.hide();
		 Dispatcher.get().dispatch(AppEvents.Init);
//		 Dispatcher.forwardEvent(AppEvents.Init_Shortcut);
//		 Dispatcher.forwardEvent(AppEvents.Init_AutoRun);
	}
	private void onSubmit() {
		status.setBusy("登陆中,请稍后...");
		status.show();
		getButtonBar().disable();

		// public void onSuccess(ClientPerson result) {
		// Registry.register("person", result);
		// LoginDialog.this.hide();
		// MessageBox wait=MessageBox.wait("", "", "程序启动中");
		// Dispatcher.get().dispatch(AppEvents.Init);
		// Dispatcher.forwardEvent(AppEvents.Init_Shortcut);
		// Dispatcher.forwardEvent(AppEvents.Init_AutoRun);
		// wait.close();

		// 浏览器判断
		if (!GXT.isChrome) {
			if (GXT.isIE) {
				if (!GXT.isWebKit) {
					InfoConfig config = new InfoConfig(
							"友情提示",
							"您使用的是IE浏览器，为了获得更好的体验，请安装<a target='_blank' href='http://code.google.com/chrome/chromeframe'>插件</a>");
					config.display = 10000;
					Info.display(config);
				}
			} else {
				InfoConfig config = new InfoConfig("友情提示",
						"为了获得更好的体验，请您使用谷歌或火狐浏览器");
				config.display = 100000;
				Info.display(config);
			}
		}
	}

}
